<!--
* @description:新增角色
* @fileName: TheNewRole
* @author:孟帅龙
* @date:2021-1-10
* @后台人员:孟帅龙
-->
<template>
  <div>
    <!-- 新增角色 -->
    <div class="xzjs">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- <el-form-item label="编号：" prop="RoleNumber">
          <el-input
            v-model="ruleForm.RoleNumber"
            placeholder="编号一般由js+4位数组成，如js0001"
          ></el-input>
        </el-form-item> -->
        <el-form-item label="角色名称：" prop="RoleName">
          <el-input v-model="ruleForm.RoleName"></el-input>
        </el-form-item>
        <el-form-item label="上级名称：" prop="SuperiorRole">
          <el-select
            v-model="ruleForm.SuperiorRole"
            placeholder="请选择上级名称"
          >
            <el-option label="——" value="——"></el-option>
            <el-option label="大山炮" value="大山炮"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="别名：" prop="Alias">
          <el-input v-model="ruleForm.Alias"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="goBack">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        RoleName: "",
        SuperiorRole: "",
        // RoleNumber: "",
        Alias: "",
      },
      rules: {
        RoleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
        SuperiorRole: [
          { required: true, message: "请选择上级名称", trigger: "change" },
        ],
        Alias: [{ required: true, message: "请输入别名", trigger: "blur" }],
        RoleNumber: [
          { required: true, message: "请输入编号", trigger: "blur" },
          { min: 6, max: 6, message: "长度必须为6个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    goBack() {
      this.$router.push("/RoleManagement");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
          this.$axios
            .post(
              "https://localhost:44399/api/app/role/add-role",
              this.ruleForm
            )
            .then((res) => {
              if (res.data > 0) {
                this.$message.success("添加成功");
                this.$router.push("/RoleManagement");
              } else {
                this.$message.error("添加失败");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
.xzjs {
  width: 500px;
}
</style>